
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
	private static final int PORT = 1099;
	private static final String HOST = "rmi://localhost:";
	
	/**
	 * Constructs a server
	 */
	public Server(){
		try
		{
			System.out.println("Booking Server Started");
			//Bind the implementation 
			BookingService BookingImpl = new BookingServiceImpl();
			Naming.rebind(HOST + PORT + "/BookingService", (Remote) BookingImpl);
			//Standard In reading for exit command
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
			
			//May not need the loop
			String command;
			while( (command = stdIn.readLine()) != null )
			{
				if(command.toLowerCase().equals("exit")) System.exit(0);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Starting the Server");
			e.printStackTrace();
		}
	}
	
	
	public static void StartRegistry(int port) throws RemoteException{
	    try 
	    {
	        LocateRegistry.getRegistry(port).list();
	    }
	    catch (RemoteException e) 
	    { 
	        LocateRegistry.createRegistry(port);
	    }
	}
	
	
	public static void main(String args[]){
		
		try
		{
			System.out.println("Starting Registry Service..");
			StartRegistry(PORT);
		}
		catch(RemoteException e){
			e.printStackTrace();
		}
		
		System.out.println("Starting Server..");
		new Server();
	}
	
	
	
}
